package com.tanjie.blackbean.system.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanjie.blackbean.mybatis.query.PageParam;
import com.tanjie.blackbean.system.admin.model.user.MeVO;
import com.tanjie.blackbean.system.admin.model.user.UserDTO;
import com.tanjie.blackbean.system.admin.model.user.UserQuery;
import com.tanjie.blackbean.system.admin.model.user.UserVO;
import com.tanjie.blackbean.system.admin.service.IUserService;
import com.tanjie.blackbean.web.annotation.BlackBeanController;
import com.tanjie.blackbean.web.annotation.HasRole;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户表
 *
 * @author tanjie
 * @since 2022-04-20
 */
@Tag(name = "用户表")
@HasRole({"SYS"})
@BlackBeanController
@RequestMapping("user")
public class UserController {

    @Resource
    IUserService userService;

    @Operation(summary = "新增")
    @PostMapping("create")
    public UserVO createSysUserHandler(@RequestBody @Validated UserDTO dto) {
        return userService.create(dto);
    }

    @Operation(summary = "更新")
    @PostMapping("update")
    public void updateSysUserHandler(@RequestBody @Validated UserDTO dto) {
        userService.update(dto);
    }

    @Operation(summary = "删除")
    @GetMapping("delete")
    public void deleteSysUserHandler(String id) {
        userService.delete(id);
    }

    @Operation(summary = "获取详情")
    @GetMapping("detail")
    public UserVO getSysUserDetailHandler(String id) {
        return userService.detail(id);
    }

    @Operation(summary = "列表查询")
    @PostMapping("list")
    public List<UserVO> querySysUserListHandler(@RequestBody(required = false) UserQuery dto) {
        return userService.queryList(dto);
    }

    @Operation(summary = "分页查询")
    @PostMapping("page")
    public IPage<UserVO> queryPageListHandler(@RequestBody(required = false) UserQuery dto, PageParam pageParam) {
        return userService.queryPage(dto, pageParam);
    }

//  ====================================================================================================================

    @HasRole()
    @Operation(summary = "登录成功返回当前用户信息")
    @GetMapping("me")
    public MeVO getCurrentUserInfoHandler() {
        return userService.getCurrentUserInfo();
    }

}
